package com.dfdchem.imd.kaiwu.activity;

import android.os.Bundle;
import android.os.Handler;

import com.alibaba.android.arouter.launcher.ARouter;
import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.activity.base.BaseActivityUM;
import com.dfdchem.imd.kaiwu.constant.RouterPath;
import com.dfdchem.imd.kaiwu.manager.DataManager;
import com.dfdchem.imd.kaiwu.model.CommonUri;
import com.dfdchem.imd.kaiwu.util.SPUtil;
import com.dfdchem.imd.kaiwu.util.StringUtil;
import com.dfdchem.imd.kaiwu.util.VersionUtil;

import zuo.biao.library.util.Log;

/**
 * 闪屏activity，保证点击桌面应用图标后无延时响应
 * @author yqq
 */

public class SplashActivity extends BaseActivityUM {
	private static final String TAG = "SplashActivity";

	private final int postDelay = 1000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Log.d(TAG, this.getClass().getName() + " onCreate");

		setContentView(R.layout.activity_splash);

		boolean isFirstLaunch = DataManager.getInstance().isFirstLaunch(VersionUtil.getAppVersionName(this));
		Log.d(TAG, "isFirstLaunch:" + isFirstLaunch);

		if (isFirstLaunch) {
			new Handler().postDelayed(new Runnable() {

				@Override
				public void run() {
					DataManager.getInstance().isLaunched(VersionUtil.getAppVersionName(getApplicationContext()));
					toActivity(RouterPath.GUIDE_ACTIVITY, true);
				}
			}, postDelay);
		} else {
			CommonUri launchAd = new SPUtil(this).getObject("LaunchAd", CommonUri.class);
			if (launchAd != null && !StringUtil.isEmpty(launchAd.getImageUrl())) {
				new Handler().postDelayed(new Runnable() {

					@Override
					public void run() {
						ARouter.getInstance()
								.build(RouterPath.SPLASH_AD_ACTIVITY)
								.withString("imageUrl", launchAd.getImageUrl())
								.withString("imagePath", launchAd.getImagePath())
								.withString("url", launchAd.getUrl())
								.withTransition(android.R.anim.fade_in, android.R.anim.fade_out)
								.navigation(SplashActivity.this);
					}
				}, postDelay);

			} else {
				new Handler().postDelayed(new Runnable() {

					@Override
					public void run() {
						toActivity(RouterPath.MAIN_ACTIVITY, true, android.R.anim.fade_in, android.R.anim.fade_out);
					}
				}, postDelay);
			}
		}
	}


}